package com.shou.api.model.dto;

import com.fasterxml.jackson.annotation.JsonFormat;
import jakarta.validation.constraints.NotNull;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 * ClassName:DataAnalysisDTO
 * Project:SmartPark
 * Package: com.shou.api.model.dto
 * Description
 *
 * @Author Nemo
 * @Create 2025/2/21 18:13
 * @Version 1.0
 */

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class DataAnalysisDTO {
    /**
     * 停车场编号
     */
    @NotNull(message = "停车场编号不能为空")
    private String lotCode;

    /**
     * 起始时间
     */
    @NotNull(message = "起始时间不能为空")
    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private LocalDateTime startTime;

    /**
     * 结束时间
     */
    @NotNull(message = "结束时间不能为空")
    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private LocalDateTime endTime;

    /**
     * 时间单位
     */
    @NotNull(message = "时间单位不能为空")
    private String timeUnit;

    /**
     * 折线图的x轴时间单位
     */
    private List<String> lineXAxisData;

    /**
     * 入场数量数据
     */
    private List<Long> inCounts;

    /**
     * 出场数量数据
     */
    private List<Long> outCounts;

    /**
     * 条形图的x轴时间单位
     */
    private List<String> barXAxisData;

    /**
     * 车位占用率
     */
    private List<Double> occupancyRates;

    /**
     * 收入占比数据，与x轴数据对应
     */
    private List<Double> incomeRatio;

    /**
     * 总收入
     */
    private BigDecimal totalIncome;

    public static DataAnalysisDTO getDataAnalysisDTOFromMap(Map<String, Object> queryDTOMap) {
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        DataAnalysisDTO dataAnalysisDTO = new DataAnalysisDTO();
        dataAnalysisDTO.setLotCode(Optional.ofNullable(queryDTOMap.get("lotCode"))
                .map(String::valueOf)
                .orElse(null));
        dataAnalysisDTO.setStartTime(Optional.ofNullable(queryDTOMap.get("startTime"))
                .map(String::valueOf)
                .map(startTime -> {
                    try {
                        return LocalDateTime.parse(startTime, dateTimeFormatter);
                    } catch (DateTimeParseException e) {
                        e.printStackTrace();
                        return null;
                    }
                })
                .orElse(null));
        dataAnalysisDTO.setEndTime(Optional.ofNullable(queryDTOMap.get("endTime"))
                .map(String::valueOf)
                .map(endTime -> {
                    try {
                        return LocalDateTime.parse(endTime, dateTimeFormatter);
                    } catch (DateTimeParseException e) {
                        // 处理解析异常，可以选择记录日志或返回默认值
                        e.printStackTrace();
                        return null;
                    }
                })
                .orElse(null));
        dataAnalysisDTO.setTimeUnit(Optional.ofNullable(queryDTOMap.get("timeUnit"))
                .map(String::valueOf)
                .orElse(null));

        return dataAnalysisDTO;
    }
}